package eerbaar.shared.internal.message;

import java.util.HashMap;
import java.util.Map;

import eerbaar.shared.message.IMessageReaderList;
import eerbaar.shared.message.IMessageService;
import eerbaar.shared.message.Message;

public class MessageService implements IMessageService {

    private final IMessageReaderList list = new DefaultList(null);

    private final Map<String, IMessageReaderList> map = new HashMap<String, IMessageReaderList>();

    @Override
    public <T extends Message> IMessageReaderList getReaders(Class<T> type) {

        return getReaders(type.getName());
    }

    @Override
    public IMessageReaderList getReaders(String type) {

        IMessageReaderList result = this.map.get(type);
        if (result == null) {
            result = new DefaultList(this.list);
            this.map.put(type, result);
        }

        return result;
    }

    @Override
    public IMessageReaderList getReadersForAllMsgs() {

        return this.list;
    }

}
